$(document).ready(function(){
	// 监测type变化
	const location = require("../modules/location.js");
	const Modal = require("../modules/modal.js");
	const api = require("../modules/api.js");
	const modal = new Modal();

	// 获取省
	getProvinces();
	function getProvinces(){
		location.getProvinces().then(data=>{
			let list = data.result;
			$("#province").empty().append(`<option value="">省</option>`);
			list.map((item=>{
				$("#province").append(`<option value='${item}'>${item}</option>`);
			}));
		});
	};
	// 获取市
	function getCities(){
		let province = $("#province").val();
		location.getCities({province}).then(data=>{
			let list = data.result;
			$("#city").empty().append(`<option value="">市</option>`);
			list.map(item=>{
				$("#city").append(`<option value="${item}">${item}</option>`);
			});
		});
	};
	// 获取区
	function getAreas(){
		let city = $("#city").val();
		let province = $("#province").val();
		location.getAreas({city, province}).then(data=>{
			let list = data.result;
			console.log(list);
			$("#area").empty().append(`<option value="">区</option>`);
			list.map(item=>{
				$("#area").append(`<option value="${item}">${item}</option>`);
			});
		});
	};

	// 监测省变化
	$("#province").bind("change property", function(){
		let province = $(this).val();
		$("#province-text").text(province);
		$("#city-text").text("市");
		$("#city").val("");
		$("#area-text").text("区");
		$("#city").val("");
		getCities();
	});

	// 监测时变化
	$("#city").bind("change property", function(){
		let city = $(this).val();
		$("#city-text").text(city);
		$("#area-text").val("区");
		$("#area").val("");
		getAreas();
	});
	
	// 监测区变化
	$("#area").bind("change property", function(){
		let area = $(this).val();
		console.log(area);
		$("#area-text").text(area);
	});


	$("#type-select").bind("change property", function(e){
		let type = $(this).val();

		if(type == "COMMON"){
			$("#type").val("普通");
		}else if(type == "GOLD"){
			$("#type").val("金牌");
		}else if(type == "DIAMOND"){
			$("#type").val("钻石");
		};
	});

	$("#submit").click(function(){
		let type = $("#type-select").val();
		let name = $("#name").val();
		let province = $("#province").val();
		let city = $("#city").val();
		let area = $("#area").val();
		let address = $("#address").val();
		let contactName = $("#contactName").val();
		let contactPhone = $("#contactPhone").val();

		let form = {type, name, province, city, area, address, contactName, contactPhone};
		console.log(form);

		if(!type){
			return modal.confirm({title: "提示信息", content: "请选择类型！"});
		};
		if(!name){
			return modal.confirm({title: "提示信息", content: "请填写企业名称！"});
		};
		if(!province){
			return modal.confirm({title: "提示信息", content: "请选择省！"});
		};
		if(!city){
			return modal.confirm({title: "提示信息", content: "请选择市！"});
		};
		if(!area){
			return modal.confirm({title: "提示信息", content: "请选择区！"});
		};
		if(!address){
			return modal.confirm({title: "提示信息", content: "请填写地址！"});
		};
		if(!contactName){
			return modal.confirm({title: "提示信息", content: "请填写联系人！"});
		};
		if(!contactPhone){
			return modal.confirm({title: "提示信息", content: "请填写联系电话！"});
		};
		if(!/^[1][3|4|5|7|8]\d{9}$/.test(contactPhone)){
			return modal.confirm({title: "提示信息", content: "请填写合法手机号！"});
		};
		api.shop_join(form).then(res=>{
			if(res.code == "0"){
				window.location.href = "/h5/success";
			}else{
				modal.confirm({title: "提示信息", content: res.errorMsg});
			};
		});
	});
});












